package net.wanho.dao.impl;

import net.wanho.mapper.EmpMapper;
import net.wanho.dao.EmpDao;
import net.wanho.entity.Emp;
import org.wanho.jdbc.JdbcTemplate;
import org.wanho.util.RowMapper;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2024-07-30 9:08
 * Description：<描述>
 */
public class EmpDaoImpl implements EmpDao {

    private JdbcTemplate<Emp> jt = new JdbcTemplate<>();
    private RowMapper<Emp> rm = new EmpMapper();

    @Override
    public void insert(Emp emp) {
        String sql = new StringBuffer()
                .append(" insert into t_emp ")
                .append("   (name,phone,address,status) ")
                .append(" values ")
                .append("   (?,?,?,?) ")
                .toString();
        jt.update(sql, emp.getName(), emp.getPhone(), emp.getAddress(), emp.getStatus());
    }

    @Override
    public void deleteById(int id) {

    }

    @Override
    public void updateById(Emp emp) {

    }

    @Override
    public List<Emp> selectAll() {
        String sql = new StringBuffer()
                .append(" select ")
                .append("   id,name,phone,address,status ")
                .append(" from ")
                .append("   t_emp ")
                .toString();
        return jt.query(sql, rm);
    }

    @Override
    public Emp selectById(int id) {
        return null;
    }

    @Override
    public Emp selectByName(String name) {
        String sql = new StringBuffer()
                .append(" select ")
                .append("   id,name,phone,address,status ")
                .append(" from ")
                .append("   t_emp ")
                .append(" where ")
                .append("   name=? ")
                .toString();
        return jt.queryForObject(sql, rm, name);
    }

    public static void main(String[] args) {
        Emp emp = new Emp(null, "张三", "123456", "北京", 1);
        EmpDaoImpl empDao = new EmpDaoImpl();
        empDao.insert(emp);
    }
}
